package com.ruoyi.worklog.service;


import com.ruoyi.worklog.domain.WlProject;
import com.ruoyi.worklog.domain.WlWorkLog;
import com.ruoyi.worklog.domain.WlWorkLogProject;
import com.ruoyi.worklog.mapper.WlWorkLogProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.worklog.mapper.WlWorkLogMapper;
import com.ruoyi.worklog.domain.WlWorkLog;
import com.ruoyi.worklog.service.WlWorkLogService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author cw
 * @Date 2022/7/22 - 07 - 22
 * @Description com.ruoyi.worklog.service
 */
@Service
public class WlWorkLogServiceImpl implements WlWorkLogService{

    @Autowired
    private WlWorkLogMapper wlWorkLogMapper;
    @Autowired
    private WlWorkLogProjectMapper wlWorkLogProjectMapper;
    /**
     * 查询日志
     *
     * @param id 日志ID
     * @return 日志
     */
    @Override
    public WlWorkLog selectWlWorkLogById(Long id)
    {
        return wlWorkLogMapper.selectWlWorkLogById(id);
    }

    /**
     * 查询日志列表
     *
     * @param wlWorkLog 日志
     * @return 日志
     */
    @Override
    public List<WlWorkLog> selectWlWorkLogList(WlWorkLog wlWorkLog)
    {
        return wlWorkLogMapper.selectWlWorkLogList(wlWorkLog);
    }

    /**
     * 新增日志
     *
     * @param wlWorkLog 日志
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public int insertWlWorkLog(WlWorkLog wlWorkLog) {
        //插入日报的同时，也要插入 日报-项目关系表
        try {
//            System.out.println("打印："+wlWorkLog);
            wlWorkLogMapper.insertWlWorkLog(wlWorkLog);
            List<Integer> pros = new ArrayList<>();
            for (WlProject p : wlWorkLog.getProjects()) {
                if (p != null) pros.add(p.getId().intValue());
            }

            if (!pros.isEmpty()) {
                System.out.println("打印list:"+ Arrays.toString(pros.toArray()));
                wlWorkLogProjectMapper.insert(new WlWorkLogProject(wlWorkLog.getId(), pros));
            }
        }catch (Exception e){
            throw new RuntimeException(e.getMessage()+"插入失败");
        }
        return 1;
    }

    /**
     * 修改日志
     *
     * @param wlWorkLog 日志
     * @return 结果
     */
    @Override
    public int updateWlWorkLog(WlWorkLog wlWorkLog)
    {
        return wlWorkLogMapper.updateWlWorkLog(wlWorkLog);
    }

    public int updateWlWLApproval(byte approval_status,int id){
        return wlWorkLogMapper.updateWlWorkLogApproval(approval_status,id);
    }
    /**
     * 批量删除日志
     *
     * @param ids 需要删除的日志ID
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
    public int deleteWlWorkLogByIds(Long[] ids)
    {
        try{
            wlWorkLogMapper.deleteWlWorkLogByIds(ids);
            wlWorkLogProjectMapper.deleteProjects2(ids);
        }catch (Exception e){
            throw  new RuntimeException(e.getMessage()+"删除失败");
        }
        return 1;
    }

}
